home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5709 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.0 KB  |  102 lines

  1. Newsgroups: comp.lang.c
  2. Path: news.sprintlink.net!news1!news
  3. From: rclark@iquest.net (Robert B. Clark)
  4. Subject: Re: I can't print the date in the format I want.
  5. X-Nntp-Posting-Host: ind-004-236-169.iquest.net
  6. Message-ID: <3129e355.114134@news.iquest.net>
  7. Sender: news@iquest.net (News Admin)
  8. Organization: IQuest Internet, Inc.
  9. X-Newsreader: Forte Agent .99d/16.182
  10. References: <4g5nbf$8s0@newsbf02.news.aol.com>
  11. Date: Tue, 20 Feb 1996 15:45:41 GMT
  12.  
  13. On 17 Feb 1996 18:11:43 -0500, asciizero@aol.com (ASCII zero) wrote:
  14.  
  15. >I am trying to use strftime() to format the date into YY-MM-DD. The format
  16. >seems to be working but I am unable to actually print the formatted
  17. >string. It compiles just fine using gcc. What have I overlooked?
  18.  
  19. You need to check your compiler and make sure that you've not disabled
  20. those oh-so-useful warning messages:
  21.  
  22. >main()
  23.  
  24. NC.
  25.  
  26. On second thought, I'll comment anyway.  Your function ought to return
  27. some sort of value, if for no other reason than to reduce the flamewars
  28. people tend to get into over various permutations of the main()
  29. declaration:
  30.  
  31.     int main(void)
  32.  
  33. > debug = strftime(str, 50, "The current date is %y-%m-%d", ptr);
  34.  
  35. "Possible use of 'str' before definition."
  36.  
  37. You've got a char pointer that points to nothing in particular--you need
  38. to allocate space for this string either by malloc'ing it, or by
  39. explicitly defining a char array of sufficient size.
  40.  
  41. > printf("-> %i", debug);          /* can we get the length of the
  42. >formatted string? */
  43.  
  44. Yes.  Use the strlen() function (#include <string.h>).
  45.  
  46. > getchar();                           /* OK up to this point? */
  47.  
  48. "Code has no effect."
  49.  
  50. Doesn't do anything but wait for the user to press a key.  If that was
  51. your intent, it would be better to express this as
  52.  
  53.     if getchar();
  54.  
  55. (and possibly wrap it in a suitably-named macro) if nothing else.
  56.  
  57. > printf("%s", str);
  58. >
  59. >}
  60.  
  61. "Function should return a value."
  62.  
  63. See my comments under main().
  64.  
  65. Here is a slight revision of your code, with the disastrous null pointer
  66. taken care of by explicitly declaring a char array for str instead of
  67. just a char pointer:
  68.  
  69. #include <stdio.h>
  70. #include <time.h>
  71. #include <string.h>         /* For strlen() function */
  72. #define SLEN        50    /* Allocate 50 chars for str */
  73.  
  74. int main(void)    
  75. {
  76.    struct tm *ptr;
  77.    time_t lt;
  78.    char str[SLEN];    /* Explicitly allocate 50 chars for str */
  79.    int debug;
  80.  
  81.    lt = time(NULL);
  82.    ptr = localtime(<);
  83.    printf("\nThe system clock shows: %s\n\n",asctime(ptr));
  84.    debug=strftime(str,SLEN,"The current date is %y-%m-%d",ptr);
  85.    printf("\nThe value of 'debug' is %d"
  86.           "\nThe length of 'str' is %d", debug,strlen(str));
  87.    printf("\nFormatted time string: \"%s\"",str);
  88.    return debug;
  89. }
  90.  
  91. Running this program should yield the following:
  92.  
  93. The system clock shows: Tue Feb 20 10:35:42 1996
  94.  
  95. The value of 'debug' is 28
  96. The length of 'str' is 28
  97. Formatted time string: "The current date is 96-02-20"
  98. --
  99. Robert B. Clark <rclark@iquest.net>
  100. "Be wary of strong spirits.  It can make you shoot at tax collectors...
  101. and miss." --RAH
  102.